package com.dyh.quickdemozwei.mdh;


import com.alibaba.fastjson.JSONObject;

public interface FieldReaders {

    interface FieldReaderJson extends FieldReader<JSONObject> {}

    FieldReaderJson json  = (body, clue, extendArgs) -> {
        if (body==null)
            return null;
        if (clue.path()==null||clue.path().length()<=0)
            return body.get(clue.key());

        PathAnalyzer pathAnalyzer =
                SmartArgUtils.elect(SmartArgUtils.softArgGet(0,extendArgs,PathAnalyzer.class),
                        PathAnalyzers.defaultPathAnalyzer);

        String[] pathArry =  pathAnalyzer.work(clue.path());

        JSONObject node =body;
        for (String path : pathArry) {
           Object raw =  node.get(path);
           if (raw instanceof JSONObject){
               node = (JSONObject)raw;
           }
           else
               return null;
        }
        return node.get(clue.key());
    };
}
